package com.googlecode.bip.xml.importe;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.NamingException;

/**
 *
 * @author Claude CHOISNET
 */
class SQLQuestionLocal  extends Commiter implements Commitable
{
    SQLQuestion     parent;
    private String  idlocal;
    private String  questionname;
    private String  questiondescription;

    private List<SQLValueDescription> childSQLValueDescriptionList = new ArrayList<SQLValueDescription>();

    SQLQuestionLocal()
    {
    }

    void setParent( SQLQuestion parent )
    {
        if( parent == null ) {
            throw new NullPointerException( "parent SQLQuestion is null" );
            }

        this.parent = parent;
    }

    void setIdLocal( String idlocal )
    {
        if( idlocal == null ) {
            throw new NullPointerException( "idlocal is null" );
            }
        this.idlocal = idlocal;
    }

    public String getIdLocal()
    {
        return idlocal;
    }

    void setQuestionName( String name )
    {
        if( name == null ) {
            throw new NullPointerException( "name is null" );
            }

        this.questionname = name;
    }

    void setQuestionDescription( String description )
    {
        if( description == null ) {
            throw new NullPointerException( "description is null" );
            }

        this.questiondescription = description;
    }

    public void add( SQLValueDescription child )
    {
        if( child == null ) {
            throw new NullPointerException("child SQLQuestion is null");
            }

        childSQLValueDescriptionList.add( child );
    }

    @Override
    public void commit() throws NamingException, SQLException
    {
        if( idlocal == null ) {
            throw new NullPointerException( "idlocal is null" );
            }
        if( questionname == null ) {
            throw new NullPointerException( "questionname is null" );
            }
        if( questiondescription == null ) {
            throw new NullPointerException( "questiondescription is null" );
            }

        String sql = "REPLACE INTO `questions_local` (`idquestion`, `idlocal`, `questionname`, `questiondescription`) VALUES ('"
            + parent.getIdQuestion()
            + "','"
            + idlocal
            + "','"
            + questionname
            + "','"
            + questiondescription
            + "');";

        super.performeCommit( sql );

        for( SQLValueDescription c : childSQLValueDescriptionList ) {
            c.setParent(parent);
            c.setParent(this);
            c.commit();
            }
    }

}
